package com.ly.demo.lydemo.ue;

import com.ly.common.bean.Result;
import com.ly.upload.web.UploadController;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;

/**
 * @program: ly-common
 * @description:
 * @author: 作者
 * @create: 2022-10-14 16:22
 */
@RestController
public class UeDo {

    @Resource
    private UploadController uploadController;

    @RequestMapping(value = "/ue/jsp/controller.jsp", params = "action=config")
    public ConfigInfo controller(String action) {
        return new ConfigInfo("up", "http://127.0.0.1/", "file");
    }

    @RequestMapping(value = "/ue/jsp/controller.jsp", params = "action=up")
    public UeResult controller1(String action, @RequestParam("file") MultipartFile file) throws IOException {
        System.out.println(file.getOriginalFilename());
        Result res = uploadController.upload(file);
        if (res.getCode() == 1) {
            return new UeResult("SUCCESS", res.getData().toString().substring(1), file.getOriginalFilename(), file.getOriginalFilename());
        } else {
            return new UeResult("FAILURE", null, null, null);
        }
    }
}

@Data
@AllArgsConstructor
@NoArgsConstructor
class ConfigInfo {
    private String imageActionName;
    private String imageUrlPrefix;
    private String imageFieldName;
}

@Data
@AllArgsConstructor
@NoArgsConstructor
class UeResult {
    private String state;
    private String url;
    private String title;
    private String original;
}
